using UnityEngine;
using System.Collections;

public class EnemyPatrolState : State<EnemyControl> 
{

    #region Singleton

    private static EnemyPatrolState instance = new EnemyPatrolState();
    public static EnemyPatrolState Instance { get { return instance; } }
    private EnemyPatrolState() { }

    #endregion

    #region State

    public override void OnEnter(EnemyControl behaviour)
    {
        behaviour.renderer.material.color = Color.green;
        behaviour.MoveTo(behaviour.CurrentWaypoint.position, behaviour.walkSpeed);
    }

    public override void OnUpdate(EnemyControl behaviour)
    {
        if (behaviour.IsPlayerInViewRange())
            behaviour.State = EnemyAlertState.Instance;
        else if (behaviour.IsOverPoint(behaviour.CurrentWaypoint.position, behaviour.waypointOffset))
        {
            behaviour.NextWaypoint();
            behaviour.MoveTo(behaviour.CurrentWaypoint.position, behaviour.walkSpeed);
        }
    }

    public override void OnExit(EnemyControl behaviour)
    {
        behaviour.Controller.ResetPath();
    }

    public override void OnAction(string name, EnemyControl behaviour, object value)
    {
        switch (name)
        {
            case ActionConstants.TRIGGER:
                this.OnTrigger(behaviour, value as Collider);
                break;
        }
    }

    #endregion

    #region Methods

    private void OnTrigger(EnemyControl behaviour, Collider other)
    {
        if (other.tag == TagConstants.TAG_SOUND_AREA)
        {
            behaviour.SoundHeard(other.transform.position);
            behaviour.State = EnemyCheckState.Instance;
        }
    }

    #endregion
}
